package com.vca.admin.controller;


import com.vca.common.model.user.User;
import com.vca.common.page.CommonPage;
import com.vca.common.request.*;
import com.vca.common.response.UserResponse;
import com.vca.common.result.CommonResult;
import com.vca.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/user")
@Api(tags = "会员管理")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页显示用户表
     * @param request 搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:user:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserResponse>> getList(@ModelAttribute @Validated UserSearchRequest request,
                                                          @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<UserResponse> userCommonPage = CommonPage.restPage(userService.getList(request, pageParamRequest));
        return CommonResult.success(userCommonPage);
    }

    @PreAuthorize("hasAuthority('admin:user:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/aaa", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserResponse>> aaa(@ModelAttribute @Validated UserSearchRequest request,
                                                          @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<UserResponse> userCommonPage = CommonPage.restPage(userService.getList(request, pageParamRequest));
        return CommonResult.success(userCommonPage);
    }

    /**
     * 修改用户表
     * @param id integer id
     * @param userRequest 修改参数
     */
    @PreAuthorize("hasAuthority('admin:user:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id, @RequestBody @Validated UserUpdateRequest userRequest) {
        userRequest.setUid(id);
        if (userService.updateUser(userRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改用户手机号
     * @param id 用户uid
     * @param phone 手机号
     */
    @PreAuthorize("hasAuthority('admin:user:update:phone')")
    @ApiOperation(value = "修改用户手机号")
    @RequestMapping(value = "/update/phone", method = RequestMethod.GET)
    public CommonResult<String> updatePhone(@RequestParam(name = "id") Integer id, @RequestParam(name = "phone") String phone) {
        if (userService.updateUserPhone(id, phone)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 用户详情
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:user:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<User> info(@RequestParam(value = "id") Integer id) {
        return CommonResult.success(userService.getInfoByUid(id));
    }



    /**
     * 操作积分
     */
    @PreAuthorize("hasAuthority('admin:user:operate:founds')")
    @ApiOperation(value = "积分余额")
    @RequestMapping(value = "/operate/founds", method = RequestMethod.GET)
    public CommonResult<Object> founds(@Validated UserOperateIntegralMoneyRequest request) {
        if (userService.updateIntegralMoney(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 添加内部员工
     * @param request
     * @return 成功 失败
     * @author Li
     * @date 2022/11/09 11:08
     */
    @PreAuthorize("hasAuthority('admin:user:add:internal')")
    @ApiOperation("注册内部员工")
    @PostMapping("/addInternalUser")
    public CommonResult addInternalUser(@RequestBody @Validated UserAddInternalRequest request){
       return userService.addInternalUser(request)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 修改用户邮箱
     * @param id 用户uid
     * @param email 邮箱
     */
    @PreAuthorize("hasAuthority('admin:user:update:email')")
    @ApiOperation(value = "修改用户邮箱")
    @RequestMapping(value = "/update/email", method = RequestMethod.GET)
    public CommonResult updateEmail(@RequestParam(name = "id") Integer id, @RequestParam(name = "email") String email) {
        return userService.updateUserEmail(id, email)?CommonResult.success():CommonResult.failed();
    }


    /**
     * 获取用户当月的消费记录
     * @param id
     * @return
     * @author Li
     * @date 2022/11/09 13:16
     */
    @PreAuthorize("hasAuthority('admin:user:get:expense:calendar')")
    @ApiOperation(value = "获取用户当月的消费记录")
    @GetMapping("/getExpenseCalendar")
    public CommonResult getExpenseCalendar(@RequestParam("id")Integer id,@ModelAttribute PageParamRequest pageParamRequest){
        CommonPage userCommonPage = CommonPage.restPage(userService.getExpenseCalendar(id,pageParamRequest));
        return CommonResult.success(userCommonPage);
    }

    /**
     * 获取用户总订单详情
     * @param id
     * @return
     * @author Li
     * @date 2022/11/09 16::32
     */
    @PreAuthorize("hasAuthority('admin:user:get:order:Info')")
    @ApiOperation(value = "获取用户总订单详情")
    @GetMapping("/getUserOrderInfo")
    public CommonResult getUserOrderInfo(@RequestParam("id")Integer id){
        return CommonResult.success(userService.getUserOrderInfo(id));
    }

}



